<?php

// +----------------------------------------------------------------------
// | Copyright (c) 2020 All rights reserved.
// +----------------------------------------------------------------------
// | User: Simony <1362229026@qq.com>
// | wechat: Shen86579595
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\controller\Common;
use think\Db;

class Chat extends Common
{

    public function privateChatList()
    {
        //params
        $type = input('param.type', 1); //所有，1今天

        //where
        $where = [];
//        $where[] = ['receive_type', 'eq', 1];//0全体会员 1会员 2组

        switch ($type) {
            case 1: //私聊
                $where[] = ['msg_type', 'eq', 0];
                break;
            case 2: //今日私聊
                $where[] = ['msg_type', 'eq', 0];
                $where[] = ['time', 'egt', strtotime(date('Y-m-d'))];
                break;
            case 3: //通知
                $where[] = ['msg_type', 'neq', 0];
                break;
            case 4: //今日通知
                $where[] = ['msg_type', 'neq', 0];
                $where[] = ['time', 'egt', strtotime(date('Y-m-d'))];
                break;
            default:
                break;
        }

        //model
        $chatRecordModel = model('common/chat/ChatRecord');

        //getData
        $list = $chatRecordModel->getList($where, 10, '*', 'time desc');

        //assign
        $this->assign('list', $list);
        $this->assign('types', $chatRecordModel->types);
        $this->assign('statuss', $chatRecordModel->statuss);
        $this->assign('msg_types', $chatRecordModel->msg_types);
        $this->assign('url', url('Chat/privateChatList', ['field' => 'create_time', 'value' => strtotime('Y-m-d')]));
        $this->assign('page', $chatRecordModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 聊天记录表列表
     * @return mixed
     */
    public function recordList()
    {
        //params
        $field = input('get.field', '');
        $value = input('get.value', '');
        $search_field = input('get.search_field', '');
        $search_value = input('get.search_value', '');

        //where
        $where = [];

        //judge
        if ($field) $where[$field] = $value;
        if ($search_field) $where[$search_field] = $search_value;

        //model
        $chatRecordModel = model('common/chat/ChatRecord');

        //getData
        $list = $chatRecordModel->getList($where, 10);

        //assign
        $this->assign('list', $list);
        $this->assign('page', $chatRecordModel->page_info->render());
        return $this->fetch();
    }


    /**
     * User:Simony
     * Function: 聊天记录表添加修改
     */
    public function recordAdd()
    {
        //params
        $id = input('param.id/d', '');

        //model
        $chatRecordModel = model('common/chat/ChatRecord');
        $data = $chatRecordModel->getInfoByID($id);

        if ($this->request->isAjax()) {

            //post params
            $ajax_data = $this->request->post();
            /*$validate = validate('ChatRecord');
            if (!$validate->check($ajax_data)) {
               return ['err' => $validate->getError()];
           }*/
            //judge
            if ($id && !$data) ds_json_encode('err', '数据不存在');

            $res = $id ? $chatRecordModel->edit(['id' => $id], $ajax_data) : $chatRecordModel->add($ajax_data);
            $res ? ds_json_encode('ret', '操作成功') : ds_json_encode('err', '操作失败');
        }
        $this->assign('data', $data);
        return $this->fetch();
    }

}